{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "尝试采用不同的权重初始化方式, 抛弃零初始化."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/Users/qi/anaconda3/lib/python3.6/site-packages/h5py/__init__.py:34: FutureWarning: Conversion of the second argument of issubdtype from `float` to `np.floating` is deprecated. In future, it will be treated as `np.float64 == np.dtype(float).type`.\n",
      "  from ._conv import register_converters as _register_converters\n"
     ]
    }
   ],
   "source": [
    "# 导入模块\n",
    "import argparse # 命令行解析包\n",
    "import sys # 一系列 Python 运行环境的变量和函数\n",
    "\n",
    "from tensorflow.examples.tutorials.mnist import input_data\n",
    "\n",
    "import tensorflow as tf\n",
    "\n",
    "FLAGS = None"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Extracting ./train-images-idx3-ubyte.gz\n",
      "Extracting ./train-labels-idx1-ubyte.gz\n",
      "Extracting ./t10k-images-idx3-ubyte.gz\n",
      "Extracting ./t10k-labels-idx1-ubyte.gz\n"
     ]
    }
   ],
   "source": [
    "# 导入数据\n",
    "data_dir = \"./\"\n",
    "mnist = input_data.read_data_sets(data_dir, one_hot=True)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "# 创建模型\n",
    "\n",
    "# 采用常数 0.1 初始化权重\n",
    "x = tf.placeholder(tf.float32, [None, 784])\n",
    "W_1 = tf.Variable(tf.constant(0.1, shape=[784, 128]))\n",
    "b_1 = tf.Variable(tf.random_normal([128]))\n",
    "logit_1 = tf.matmul(x, W_1) + b_1\n",
    "y_1 = tf.nn.sigmoid(logit_1)\n",
    "W_2 = tf.Variable(tf.constant(0.1, shape=[128, 10]))\n",
    "b_2 = tf.Variable(tf.random_normal([10]))\n",
    "y = tf.matmul(y_1, W_2) + b_2\n",
    "\n",
    "# 采用截断正态分布初始化权重\n",
    "# x = tf.placeholder(tf.float32, [None, 784])\n",
    "# W_1 = tf.Variable(tf.truncated_normal([784, 128], stddev=0.1))\n",
    "# b_1 = tf.Variable(tf.truncated_normal([128], stddev=0.1))\n",
    "# logit_1 = tf.matmul(x, W_1) + b_1\n",
    "# y_1 = tf.nn.sigmoid(logit_1)\n",
    "# W_2 = tf.Variable(tf.truncated_normal([128, 10], stddev=0.1))\n",
    "# b_2 = tf.Variable(tf.truncated_normal([10], stddev=0.1))\n",
    "# y = tf.matmul(y_1, W_2) + b_2\n",
    "\n",
    "# 采用 N(0,1/sqrt(nin)) 初始化权重\n",
    "# x = tf.placeholder(tf.float32, [None, 784])\n",
    "# W_1 = tf.Variable(tf.random_normal([784, 128]) / tf.sqrt(784.0))\n",
    "# b_1 = tf.Variable(tf.random_normal([128]))\n",
    "# logit_1 = tf.matmul(x, W_1) + b_1\n",
    "# y_1 = tf.nn.sigmoid(logit_1)\n",
    "# W_2 = tf.Variable(tf.random_normal([128, 10]) / tf.sqrt(128.0))\n",
    "# b_2 = tf.Variable(tf.random_normal([10]))\n",
    "# y = tf.matmul(y_1, W_2) + b_2\n",
    "\n",
    "# 采用 MSRA 初始化权重\n",
    "# x = tf.placeholder(tf.float32, [None, 784])\n",
    "# W_1 = tf.Variable(tf.random_normal([784, 128]) / tf.sqrt(784.0 / 2))\n",
    "# b_1 = tf.Variable(tf.random_normal([128]))\n",
    "# logit_1 = tf.matmul(x, W_1) + b_1\n",
    "# y_1 = tf.nn.sigmoid(logit_1)\n",
    "# W_2 = tf.Variable(tf.random_normal([128, 10]) / tf.sqrt(128.0 / 2))\n",
    "# b_2 = tf.Variable(tf.random_normal([10]))\n",
    "# y = tf.matmul(y_1, W_2) + b_2"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "定义 ground truth"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "y_ = tf.placeholder(tf.float32, [None, 10])"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "接下来计算交叉熵, 使用系统函数. softmax_cross_entropy_with_logits 的 logits 参数是未经激活的 Wx+b"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "WARNING:tensorflow:From <ipython-input-5-f9bad1b19477>:1: softmax_cross_entropy_with_logits (from tensorflow.python.ops.nn_ops) is deprecated and will be removed in a future version.\n",
      "Instructions for updating:\n",
      "\n",
      "Future major versions of TensorFlow will allow gradients to flow\n",
      "into the labels input on backprop by default.\n",
      "\n",
      "See tf.nn.softmax_cross_entropy_with_logits_v2.\n",
      "\n"
     ]
    }
   ],
   "source": [
    "cross_entropy = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(labels=y_, logits=y))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "生成一个训练 step"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "train_step = tf.train.GradientDescentOptimizer(learning_rate=0.5).minimize(cross_entropy)\n",
    "\n",
    "sess = tf.Session()\n",
    "init_op = tf.global_variables_initializer()\n",
    "sess.run(init_op)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "仍然调用系统提供的读取数据, 为我们取得一个 batch. 然后我们运行 3k 个 step(5 epochs), 对权重进行优化."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "# 训练\n",
    "for _ in range(3000):\n",
    "    batch_xs, batch_ys = mnist.train.next_batch(100)\n",
    "    sess.run(train_step, feed_dict={x: batch_xs, y_: batch_ys})"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0.9113\n"
     ]
    }
   ],
   "source": [
    "# 测试训练完的模型\n",
    "correct_prediction = tf.equal(tf.argmax(y, 1), tf.argmax(y_, 1))\n",
    "accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))\n",
    "print(sess.run(accuracy, feed_dict={x: mnist.test.images, y_: mnist.test.labels}))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "改变初始化方式后, 准确率提升明显.  \n",
    "以下为各种初始化方式下的准确率:  \n",
    "常数 0.1 初始化 0.9113  \n",
    "截断正态分布初始化 0.9566  \n",
    "N(0,1/sqrt(nin)) 初始化 0.9562  \n",
    "MSRA 初始化 0.9555  \n",
    "后面三张初始化效果都不错. 之后都采用 Xavier 即 N(0,1/sqrt(nin)) 初始化和 MSRA 初始化."
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
